//
//  PlayWarGame.m
//  WarGame
//
//  Created by Jacob V.V. Goldberg on 6/27/12.
//  Copyright (c) 2012 FutureSight Technologies. All rights reserved.
//

#import "PlayWarGame.h"
#import "WarGameDecks.h"

@interface PlayWarGame()
//private interface
@property (weak, nonatomic) IBOutlet UILabel *botDisplay;
@property (weak, nonatomic) IBOutlet UILabel *playerCard;
@property (weak, nonatomic) IBOutlet UIButton *flipBtn;
@property (strong, nonatomic) IBOutlet UILabel *resultDisplay;
@property (strong, nonatomic) NSDictionary *conversionTable;
@property (strong, nonatomic) NSString *mode;
@property (strong, nonatomic) WarGameDecks *cards;

@end

@implementation PlayWarGame
@synthesize botDisplay = _botDisplay;
@synthesize playerCard = _playerCard;
@synthesize flipBtn = _flipBtn;
@synthesize resultDisplay = _resultDisplay;
@synthesize conversionTable = _conversionTable;
@synthesize mode = _mode;
@synthesize cards = _cards;

-(void) viewDidLoad {
	[super viewDidLoad];
	self.cards = [[WarGameDecks alloc] init];
	[self.cards shuffleFullDeck];
	
	self.mode = @"flip";
	[self.flipBtn setTitle:@"Flip" forState:UIControlStateNormal];
	int bcards = [self.cards getBotDeckSize], pcards = [self.cards getPlayerDeckSize];
	
	[self.botDisplay setText:[NSString stringWithFormat:@"%@ cards", [NSNumber numberWithInt:bcards]]];
	[self.playerCard setText:[NSString stringWithFormat:@"%@ cards", [NSNumber numberWithInt:pcards]]];
	[self.resultDisplay setText:@""];
}
- (void)viewDidUnload {
	[self setBotDisplay:nil];
	[self setPlayerCard:nil];
	[self setFlipBtn:nil];
	[self setResultDisplay:nil];
	[super viewDidUnload];
}
- (IBAction)flip: (UIButton *) sender {
	if (self.mode == @"flip") {
		//show the top card of both decks
		int bcard = [self.cards pullFromTopOfBotDeck];
		int pcard = [self.cards pullFromTopOfPlayerDeck];
		
		NSString *bStr = [self.conversionTable objectForKey:[NSString stringWithFormat:@"%d", bcard]];
		NSString *pStr = [self.conversionTable objectForKey:[NSString stringWithFormat:@"%d", pcard]];
		
		if ([bStr rangeOfString:@"♥"].location != NSNotFound || [bStr rangeOfString:@"♦"].location != NSNotFound) {
			[self.botDisplay setTextColor:[UIColor redColor]];
		} else {
			[self.botDisplay setTextColor:[UIColor blackColor]];
		}
		if ([pStr rangeOfString:@"♥"].location != NSNotFound || [pStr rangeOfString:@"♦"].location != NSNotFound) {
			[self.playerCard setTextColor:[UIColor redColor]];
		} else {
			[self.playerCard setTextColor:[UIColor blackColor]];
		}
		
		[self.botDisplay setText:bStr];
		[self.playerCard setText:pStr];
		//check who won
		if ((pcard + 12) % 13 >= (bcard + 12) % 13) {
			[self.resultDisplay setText:@"Player won"];
			
			[self.cards putOnBottomOfPlayerDeck:pcard];
			[self.cards putOnBottomOfPlayerDeck:bcard];
		} else if ((pcard + 12) % 13 < (bcard + 12) % 13) {
			[self.resultDisplay setText:@"Bot won"];
			
			[self.cards putOnBottomOfBotDeck:bcard];
			[self.cards putOnBottomOfBotDeck:pcard];
		} else if ((pcard + 12) % 13 == (bcard + 12) % 13) {
			[self.resultDisplay setText:@"Error"];
		}
		[sender setTitle:@"Continue" forState:UIControlStateNormal];
		self.mode = @"continue";
	} else if (self.mode == @"continue") {
		[self.playerCard setTextColor:[UIColor blackColor]];
		[self.botDisplay setTextColor:[UIColor blackColor]];
		
		[sender setTitle:@"Flip" forState:UIControlStateNormal];
		int bcards = [self.cards getBotDeckSize], pcards = [self.cards getPlayerDeckSize];
		
		[self.botDisplay setText:[NSString stringWithFormat:@"%@ cards", [NSNumber numberWithInt:bcards]]];
		[self.playerCard setText:[NSString stringWithFormat:@"%@ cards", [NSNumber numberWithInt:pcards]]];
		self.mode = @"flip";
		
		[self.resultDisplay setText:@""];
		
		if ([self.cards getBotDeckSize] == 0) {
			[sender setEnabled:NO];
			[sender setTitle:@"Player won!" forState:UIControlStateNormal];
			[sender setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
		}
		if ([self.cards getPlayerDeckSize] == 0) {
			[sender setEnabled:NO];
			[sender setTitle:@"Bot won!" forState:UIControlStateNormal];
			[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
		}
	} else {
		NSLog(@"Error!");
	}
}
-(NSDictionary *) conversionTable {
    if(!_conversionTable) {
        //create the dictionary
        _conversionTable = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"A ♥", @"0", @"2 ♥", @"1", @"3 ♥", @"2", @"4 ♥", @"3", @"5 ♥", @"4", @"6 ♥", @"5", @"7 ♥", @"6", @"8 ♥", @"7", @"9 ♥", @"8", @"10 ♥", @"9", @"J ♥", @"10", @"Q ♥", @"11", @"K ♥", @"12", @"A ♠", @"13", @"2 ♠", @"14", @"3 ♠", @"15", @"4 ♠", @"16", @"5 ♠", @"17", @"6 ♠", @"18", @"7 ♠", @"19", @"8 ♠", @"20", @"9 ♠", @"21", @"10 ♠", @"22", @"J ♠", @"23", @"Q ♠", @"24", @"K ♠", @"25", @"A ♦", @"26", @"2 ♦", @"27", @"3 ♦", @"28", @"4 ♦", @"29", @"5 ♦", @"30", @"6 ♦", @"31", @"7 ♦", @"32", @"8 ♦", @"33", @"9 ♦", @"34", @"10 ♦", @"35", @"J ♦", @"36", @"Q ♦", @"37", @"K ♦", @"38",@"A ♣", @"39", @"2 ♣", @"40", @"3 ♣", @"41", @"4 ♣", @"42", @"5 ♣", @"43", @"6 ♣", @"44", @"7 ♣", @"45", @"8 ♣", @"46", @"9 ♣", @"47", @"10 ♣", @"48", @"J ♣", @"49", @"Q ♣", @"50", @"K ♣", @"51", nil];
        
    }
    return _conversionTable;
}
@end